package com.lovestudying.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lovestudying.dao.ReplyDao;
import com.lovestudying.dao.Impl.ReplyDaoImpl;
import com.lovestudying.entity.Comment;
import com.lovestudying.entity.Reply;
import com.lovestudying.service.ReplyService;
import com.lovestudying.util.PageInfo;

/**
 * 回复(Reply)的业务逻辑
 * 
 * @author OldDeer
 *
 */
public class ReplyServiceImpl implements ReplyService {
	ReplyDao ReplyDao = new ReplyDaoImpl();

	@Override
	public Boolean postReply(String replyId, String createTime, String accountId, String replyContent, String replyTo,
			String parentComment) {
		Map<String, Object> Value_Param = new HashMap<String, Object>();
		// TODO 自动生成的方法存根
		Value_Param.put("reply_id", replyId);
		Value_Param.put("create_time", createTime);
		Value_Param.put("account_id", accountId);
		Value_Param.put("reply_content", replyContent);
		Value_Param.put("reply_to", replyTo);
		Value_Param.put("parent_comment", parentComment);
		if (ReplyDao.InsertReply(Value_Param) == 1) {
			return true;
		} else {
			return false;
		}
	}

	 @Override
	public PageInfo<Reply> getReplyToInfoBy(String parentGroupUuid) {
		Map<String, Object> Where_Param = new HashMap<>();
		Where_Param.put("reply.parent_comment", parentGroupUuid);
		Where_Param.put("reply.reply_state", 1);
		PageInfo<Reply> Reply = ReplyDao.findReplyUniAccount(Where_Param, null, null, null);

		for (Reply singleReply : Reply.getData()) {
			Map<String, Object> Where_Param2 = new HashMap<>();
			Where_Param2.put("reply_id", singleReply.getReply_to());
			List<Reply> data = ReplyDao.findReplyUniAccount(Where_Param2, null, null, null).getData();
			if (data.size() != 0) {
				singleReply.setReply_to_account(data.get(0).getPost_account());
			}
		}

		return Reply;
	}

	@Override
	public Integer getReplyNumByParent(String parentGroupUuid) {
		return getReplyToInfoBy(parentGroupUuid).getData().size();
	}

	@Override
	public PageInfo<Reply> getReplyToInfoBy(String parentGroupUuid, String orderParam, String desc, Integer pageSize,
			Integer pageNum) {
		Map<String, Object> whereParams = new HashMap<>();
		Map<String, Object> orderParams = new HashMap<>();
		whereParams.put("reply.parent_comment", parentGroupUuid);
		whereParams.put("reply.reply_state", 1);
		orderParams.put(orderParam, desc);
		PageInfo<Reply> Reply = ReplyDao.findReplyUniAccount(whereParams, orderParams, pageSize, pageNum);

		for (Reply singleReply : Reply.getData()) {
			Map<String, Object> Where_Param2 = new HashMap<>();
			Where_Param2.put("reply_id", singleReply.getReply_to());
			List<Reply> data = ReplyDao.findReplyUniAccount(Where_Param2, null, null, null).getData();
			if (data.size() != 0) {
				singleReply.setReply_to_account(data.get(0).getPost_account());
			}
		}
		return Reply;
	}

}
